package com.innodata.riskEngine.controller;

import com.innodata.riskEngine.aspect.Timer;
import com.innodata.riskEngine.service.LoadTesterService;
import com.innodata.riskEngine.vo.LoadTestReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author liboshuai
 * @Date 2023/11/2 17:06
 */
@Api("压测controller")
@Slf4j
@RestController
@RequestMapping("/loadTester")
public class LoadTesterController {
    @Autowired
    private LoadTesterService loadTesterService;

    @Value("${env}")
    private String env;

    @Timer
    @ApiOperation("访问测试")
    @PostMapping("/test")
    public String test() {
        return "当前环境为：" + env;
    }


        /**
         * 压力测试
         * @return 结果
         */
    @Timer
    @ApiOperation("压力测试")
    @PostMapping("/loadTest")
    public String loadTest(@RequestBody LoadTestReq loadTestReq) {
        try {
            loadTesterService.loadTest(loadTestReq);
        } catch (Exception e) {
            log.error("loadTest >>> ", e);
            return "Fail";
        }
        return "OK";
    }

    /**
     * 从指定的 Kafka 主题中消费最后 N 条消息。
     */
    @Timer
    @ApiOperation("查看指定Kafka主题的最近N条数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "topicName",value = "kafka主题",example = "impSendOrder",dataTypeClass = String.class,required = true),
            @ApiImplicitParam(name = "offset",value = "最新N条",example = "10",dataTypeClass = Long.class,required = true),
    })
    @GetMapping("/tailKafkaTopic/{topicName}/{offset}")
    public List<String> tailKafkaTopic(@PathVariable String topicName, @PathVariable Long offset) {
        return loadTesterService.tailKafkaTopic(topicName, offset);
    }
}
